int1 is_rb_isr_enabled()
{
    return RBIE ? 1:0;
}

void error_ms (int1 ms_bit)
{
    set_timer1 (0);
    if (ms_bit) {
        out_parm.engine |= (1 << 1);         //set engine crank for MS
    } else {
        out_parm.engine &= ~(1 << 1);         //clear engine crank for MS
    }
}


void error (void)
{
    set_timer1 (0);
}

int16 timeh (void)
{
    unsigned int16 ti;
        
    ti=get_timer1();
    
#if (1 == MS_STATES) 
    error_ms(0);
#else
    error();
#endif

    if (timer1_err) {
        timer1_err = 0;
        return 0;
    }

    return ti;
}

#INT_CCP1
void ccp_isr (void)
{
    //if ((CCP1IF ==1 && CCP1IE == 1))
    if (CCP1IE == 1) {
        disable_interrupts(INT_CCP1);
    }
    return;
}
/*
        0          1          2          3
___---___---___---___---___
     0         180       360     540 
_-_______________________-_

*/
#INT_RB
void rb_isr(void)
{
    int8    change;
    static  int8    p_count_f = 0;
    static  int8    p_count_r = 0;
    change=port_b_perv ^ PORTB;
    port_b_perv=PORTB;

#if (1 == __BUILD_TYPE__)
    if (bit_test(change,5)) {
#elif (2 == __BUILD_TYPE__)
    if (bit_test(change,7)) {
#else
#error !!! Not defined build !!!
#endif

        if ((input_rpm != ACTIVE_PULSE && CCP1IE == 0)) {

#if(0 == IGN_DEBUG)
            time_f += timeh();
#else
            //time_f += ign_chanel_fall[c_p_f]= timeh();
            time_f += ign_chanel_fall[p_count_f++]= timeh();
#endif
            prepare_ignition(ign_timer_in,
                                spark_timer_tick,
                                ign_chanel_fall[c_p_f],
                                PULSE_FRONT);
            egocount ++;

            clear_interrupt(INT_CCP1);
            enable_interrupts(INT_CCP1);
            //chnage injector chanel
            if (inj_sych) {
                if (!ign_power_up) {
                    firetime(timeinj);
                } else {
                    firetime_all(timeinj_crank);
                }
                pw_count += out_parm.pw1;
                if (inj_ch < 3) {
                    inj_ch++;
                } else {
                    inj_ch = inj_sych = 0;
                }                              
            }
            if(c_p_f < CYCLE-1) {
                 c_p_f ++;
            } else {
                time_fok = time_f;
                time_f = c_p_f = 0;
            }
        } else if 
                    ((input_rpm == ACTIVE_PULSE && CCP1IE == 0)) {
            int16   tmp;

#if(0 == IGN_DEBUG)
            time_r += tmp = timeh();
#else
            //time_r += ign_chanel_rise[c_p_r]= timeh();
            time_r += ign_chanel_rise[p_count_r++]= timeh();
#endif
            prepare_ignition(ign_timer_in,
                                spark_timer_tick,
                                tmp,
                                PULSE_END);
            clear_interrupt(INT_CCP1);
            enable_interrupts(INT_CCP1);

            if (cycles) {
                cycles --;
            }

            if(c_p_r<CYCLE-1) {
                 c_p_r++;
            } else {
                 time_rok=time_r;
                 time_r=c_p_r=0;
            }
        }

#if (1 == __BUILD_TYPE__)
    } else if (bit_test(change,4)&&(input_pos==0)) {
#elif (2 == __BUILD_TYPE__)
    } else if (bit_test(change,6)&&(input_pos==0)) {
#else
#error !!! Not defined build !!!
#endif

        //wait synch here
        inj_sych = 1;
        inj_ch = 0;
        //sync for ignition debug degree 
        p_count_f = p_count_r = 0;
    }//!!!!!!!!
    clear_interrupt(INT_RB);
    return; 
}

#INT_TIMER1
void tmr1_isr(void)
{
#if (1 == MS_STATES) 
    error_ms(0);
#else
    error();
#endif

    time_rok = diff =
    time_fok = oboroti = 
    oborotit = 0;
    timer1_err = 1;

    if (deb_inj) {
        firetime_all(prime_pulse_time);
        if (flow_tst_couter) {
            flow_tst_couter--;
            if (!flow_tst_couter) {
                deb_inj = 0;
                buzz = 0;
            }
        }
    } else if (prime_pul) {
        prime_pul = 0;
        firetime_all(prime_pulse_time);
   }
    clear_interrupt(INT_TIMER1);
    return;
}

